//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Execution;
    using Model;

    internal class PreflightEngine : IPreflightEngine 
    {
        private readonly IVerificationExecutor verificationExecutor;
        private readonly IEnumerable<IVerification> verifications;
        private readonly ApplicationDetail applicationDetail;

        public PreflightEngine(IVerificationExecutor verificationExecutor, IEnumerable<IVerification> verifications, ApplicationDetail applicationDetail)
        {
            this.verificationExecutor = verificationExecutor;
            this.verifications = verifications;
            this.applicationDetail = applicationDetail;
        }

        public ApplicationDetail ApplicationDetail
        {
            get { return this.applicationDetail; }
        }

        public IEnumerable<IVerification> Verifications
        {
            get
            {
                return this.verifications;
            }
        }

        public IVerificationExecutor VerificationExecutor
        {
            get
            {
                return this.verificationExecutor;
            }
        }

        public PreflightResult RunVerifications()
        {
            return this.Execute(this.verifications);
        }

        public PreflightResult RunVerifications(IEnumerable<string> categories)
        {
            if (categories == null || categories.Any() == false)
            {
                return this.RunVerifications();
            }

            var verificationsToCheck = this.verifications.Where(verification => VerificationIsInOneOfTheseCategories(verification, categories));
            return this.Execute(verificationsToCheck);
        }

        private PreflightResult Execute(IEnumerable<IVerification> verificationsToExecute)
        {
            return new PreflightResult
                {
                    VerificationResults = this.verificationExecutor.Execute(verificationsToExecute),
                    ExecutedOnUtc = DateTime.UtcNow
                };
        }

        private static bool VerificationIsInOneOfTheseCategories(IVerification verification, IEnumerable<string> categories)
        {
            return categories.Any(item => item.ToLowerInvariant() == verification.Category.ToLowerInvariant());
        }
    }
}
